using Asp.Versioning;
using Microsoft.AspNetCore.OData;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using OData.Data;
using OData.Shared;
using OData.Shared.Extensions;
using Swashbuckle.AspNetCore.SwaggerGen;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer(); 
builder.Services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase("ODataDb"));
builder.Services.AddSwaggerGen
(
	options => options.OperationFilter<SwaggerDefaultValues>()
);
builder.Services
	.AddControllers()
    .AddOData(
    options =>
    {
	  
	    options.RouteOptions.EnableKeyInParenthesis = false;
	    options.RouteOptions.EnableNonParenthesisForEmptyParameterFunction = true;
	    options.RouteOptions.EnablePropertyNameCaseInsensitive = true;
	    options.RouteOptions.EnableQualifiedOperationCall = false;
	    options.RouteOptions.EnableUnqualifiedOperationCall = true;
	    options
		    .Select()
		    .Expand()
		    .Filter()
		    .OrderBy()
		    .Count()
		    .SetMaxTop(null);
    });
builder.Services.AddApiVersioning(options =>
{
	options.ReportApiVersions = true;
	options.Policies
		.Sunset( 1.0 )
		.Effective( 2025, 8, 9 )
		.Link( "https://docs.api.com/policy.html?api-version=1.0" )
		.Title( "API Policy" )
		.Type( "text/html" );
    
	options.Policies
		.Sunset(2.0)
		.Effective(2025, 12, 1)
		.Link("https://docs.api.com/policy.html?api-version=2.0")
		.Title("API Policy")
		.Type("text/html");
})
.AddOData(options =>
{
	options.AddRouteComponents("api");
})
.AddODataApiExplorer(options =>
{
	options.GroupNameFormat = "'v'V";
	options.SubstituteApiVersionInUrl = true;
});

var app = builder.Build();
app.MakeSureDbCreated();
if (app.Environment.IsDevelopment())
{
	app.UseSwaggerWithUi();
	app.UseODataRouteDebug();
}
app.UseRouting();
app.MapControllers();
app.Run();
